﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

using MpSoft.Library.BUSS.Contents;
using MpSoft.Library.DTO;
using MpSoft.Basic.Utils;
using MpSoft.Library.BUSS.Settings;

namespace MpSoft.Web.AdminCP.Controls.Content
{
    public partial class NewsEdit : System.Web.UI.UserControl
    {
        #region Var

        private int newsID = -1;
        private bool showlanguage = false;

        #endregion

        #region Method

        #region LanguageDefault

        private void BindControl()
        {
            SetControl();
            if (newsID > 0)
            {
                var obj = NewsBUSS.News_ByID(newsID);
                if (obj != null)
                {
                    ddlCategory.SelectVlaue = obj.CategoryID;
                    txtOrderSort.NumberIncremnet = obj.SortOrder;
                    chkIsActive.Checked = obj.IsActive;
                    chkIshome.Checked = obj.IsHome;
                    chkTypical.Checked = obj.IsTypical;

                    contentLngDefault.Name = obj.Title;
                    contentLngDefault.Description = obj.Description;
                    txtImages.Text = obj.Image;
                    contentLngDefault.Content = obj.Content;
                    contentLngDefault.MetaTitle = obj.MetaTitle;
                    contentLngDefault.MetaKeyWord = obj.MetaKeyword;
                    contentLngDefault.MetaDescription = obj.MetaDescription;
                    contentLngDefault.SeName = obj.SeName;
                }
            }
        }
        // Lưu mặc định
        private int Save()
        {

            var obj = new News();
            obj.NewsID = newsID;
            obj.CategoryID = ddlCategory.SelectVlaue;
            obj.SortOrder = txtOrderSort.NumberIncremnet;
            obj.IsActive = chkIsActive.Checked;
            obj.IsHome = chkIshome.Checked;
            obj.IsTypical = chkTypical.Checked;

            obj.Title = contentLngDefault.Name;
            obj.Description = contentLngDefault.Description;
            obj.MetaKeyword = contentLngDefault.MetaKeyWord;
            obj.MetaDescription = contentLngDefault.MetaDescription;
            obj.MetaTitle = string.IsNullOrEmpty(contentLngDefault.MetaTitle) ? obj.Title : contentLngDefault.MetaTitle;
            obj.SeName = string.IsNullOrEmpty(contentLngDefault.SeName) ? CommonHelper.FillterChar(obj.Title) : CommonHelper.FillterChar(contentLngDefault.SeName);
            obj.FormatUrl = SEOHelper.FormatUrl(ddlCategory.Category != null ? ddlCategory.Category.SeName : "tin-tuc", obj.SeName);
            obj.Content = contentLngDefault.Content;
            obj.Image = txtImages.Text;
            if (obj.NewsID > 0){
                SaveLanguage(obj.NewsID);
                return NewsBUSS.News_Update(obj);
            }
            else{
                newsID = NewsBUSS.News_Add(obj);
                SaveLanguage(newsID);
                return newsID;
            }
               
        }
        //Thông báo
        private void Message(string message)
        {
            MpSoft.Basic.Utils.CommonHelper.ResetForm(this.Controls);
            ltlMessage.Text = "<p class='message-success'><i class='glyphicon-ok'></i> " + message + "</p>";
        }
        // Thiết lập mặc định
        private void SetControl()
        {
            showlanguage = true;
            ddlCategory.DdlCategories.CssClass = "form-select";
            ddlCategory.DdlCategories.Attributes.Add("data-rule-required", "true");
            ddlCategory.DdlCategories.Attributes.Add("style", "width:auto;");
            ddlCategory.Module = "tin-tuc";
            contentLngDefault.NameNotNull = true;
            BindLanguage();
        }

        #endregion

        #region LanguageOrther

        protected string TabLanguage()
        {
            var list = LanguageBUSS.Language_Select(null);
            if (list != null)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var obj in list)
                    sb.Append(" <option value='#" + obj.LanguageCode + "'>" + obj.LanguageTitle + "</option>");
                return sb.ToString();
            }
            return "";
        }

        private void BindLanguage()
        {
            rpList.DataSource = LanguageBUSS.Language_Select(null);
            rpList.DataBind();
        }
        /// <summary>
        /// Save Object Language by ID
        /// </summary>
        /// <param name="newsid"></param>
        private void SaveLanguage(int newsid)
        {
            if (newsid < 1) return;
            if (rpList.Items.Count < 1) return;
            foreach (RepeaterItem item in rpList.Items)
            {
                var ContentLanguage = item.FindControl("ContentLanguage") as Modules.ContentLanguage;
                if (ContentLanguage != null)
                {
                    if (string.IsNullOrEmpty(ContentLanguage.Language) || string.IsNullOrEmpty(ContentLanguage.Name))
                        continue;
                    var obj = NewsLanguageBUSS.NewsLanguage_ByID(newsid, ContentLanguage.Language);
                    if (obj == null)
                        obj = new NewsLanguage();
                    obj.LanguageCode = ContentLanguage.Language;
                    obj.NewsID = newsid;
                    obj.Title = ContentLanguage.Name;
                    obj.Description = ContentLanguage.Content;
                    obj.MetaTitle = string.IsNullOrEmpty(ContentLanguage.MetaTitle) ? obj.Title : ContentLanguage.MetaTitle;
                    obj.MetaKeyword = ContentLanguage.MetaKeyWord;
                    obj.MetaDescription = ContentLanguage.MetaDescription;
                    obj.SeName = string.IsNullOrEmpty(contentLngDefault.SeName) ? CommonHelper.FillterChar(obj.Title) : contentLngDefault.SeName;
                    obj.FormatUrl = SEOHelper.FormatUrl(ddlCategory.Category != null ? ddlCategory.Category.SeName : "tin-tuc", obj.SeName);
                    if (obj.NLID > 0)
                        NewsLanguageBUSS.NewsLanguage_Update(obj);
                    else
                        NewsLanguageBUSS.NewsLanguage_Add(obj);
                }
            }
        }

        #endregion

        #endregion

        #region Event

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["itemid"] != null)
                int.TryParse(Request.QueryString["itemid"].ToString(), out newsID);
            if (!IsPostBack)
                BindControl();
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            var _categoryid = Save();
            if (_categoryid > 0)
                Message("Cập nhật thành công!");
        }
        protected void cmdSaveExit_Click(object sender, EventArgs e)
        {
            var _categoryid = Save();
            if (_categoryid > 0)
                Response.Redirect("?mod=news");
        }

        protected void rpList_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            var ContentLanguage = e.Item.FindControl("ContentLanguage") as Modules.ContentLanguage;
            if (ContentLanguage != null)
            {
                var objLanguage = e.Item.DataItem as Language;
                ContentLanguage.Language = objLanguage.LanguageCode;
                var obj = NewsLanguageBUSS.NewsLanguage_ByID(newsID, objLanguage.LanguageCode);
                if (obj != null)
                {
                    ContentLanguage.Name = obj.Title;
                    ContentLanguage.Description = obj.Description;
                    ContentLanguage.Content = obj.Content;
                    ContentLanguage.MetaTitle = obj.MetaTitle;
                    ContentLanguage.MetaKeyWord = obj.MetaKeyword;
                    ContentLanguage.MetaDescription = obj.MetaDescription;
                    ContentLanguage.SeName = obj.SeName;
                }
            }
        }

        #endregion

        #region Properties

        public bool ShowLanguage { get { return showlanguage; } }

        #endregion
    }
}